package utils.security;

import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.encoders.Hex;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;

public class encodeUtil {
    public static void main(String[] args) {
        String abcdefc = Base64.toBase64String("abcdefc".getBytes()); //
        System.out.println("base64值：" + abcdefc);
        byte[] decode = Base64.decode(abcdefc);
        String s = new String(decode);

        //    Hex 编码后的数据大小是原来的 2 倍
//    由于算法内部是对 4bit 进行转换，所以编码后的数据从显示上来看是由 0~F 这 16 个字符组成
        byte[] fffes = Hex.decode("fffe");
        String s1 = Hex.toHexString(fffes);



        System.out.println(new String(org.apache.commons.codec.digest.DigestUtils.md5Hex("Enum")));
        System.out.println(new String(org.apache.commons.codec.digest.DigestUtils.md5("Enum")));
        System.out.println(org.apache.commons.codec.binary.Hex.encodeHexString(DigestUtils.md5("Enum")));
        byte[] k= "a".getBytes();
        System.out.println(k);
        System.out.println("c31db0a1149a4bcb021e7df5a2acbc39".length());
        k=java.util.Base64.getEncoder().encode("12345asdASD".getBytes()); // byte[]类型，就是把字符化的base64.getBytes()
        System.out.println(new String(k)); // MTIzNDVhc2RBU0Q=
        System.out.println(org.apache.commons.codec.binary.Base64.encodeBase64String("12345asdASD".getBytes()));
    }


    //字节数组编码成Base64
    public static String byte2Base64(byte[] bytes){
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(bytes);
    }

    //Base64编码解码成字节数组
    public static byte[] base642Byte(String base64Key) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        return decoder.decodeBuffer(base64Key);
    }
}
